<?php

namespace App\Http\Controllers\test;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class Preg extends Controller
{
    //

    public function index(){
        //正则匹配规则示例数组
        $pregArr = [
            "tong"      =>      [
                "0"     =>      "/zoo{0,}/",
                "1"     =>      "/zoo{1,}/",
                "2"     =>      "/zoo{0,}o$/",
                "3"     =>      "/999*/"
            ],
            "Chapter"  =>[
                "0"     =>      "/Chapter [1-9][0-9]*/",
                "1"     =>      "/Chapter [0-9]*/",
                "2"     =>      "/Chapter [0-9]?/",
                "3"     =>      "/Chapter [0-9]+/",
                "4"     =>      "/Chapter [1-9][0-9]{1,2}/",
                "5"     =>      "/Chapter [0-9]?&/",
                "6"     =>      "/Chapter [0-9]+$/",
                "7"     =>      "/Chapter [1-9][0-9]{1,2}$/",
            ],
            "greedy"    =>      [
                "0"     =>      "/<.*>/",
                "1"     =>      "/<.*?>/",
                "2"     =>      "/<\w+?>/",
                "3"     =>      "/<.{4}>/",
                "4"     =>      "/<.?>/"
            ],
            "between"   =>      [
                "0"     =>      "/test\b/",
                "1"     =>      "/\btest/",
                "2"     =>      "/\btest?$/",
                "3"     =>      "/\btest+/",
            ],
            "repeat"    =>      [
                "0"     =>      "/\b([a-z]+) \1\b/",
            ],
            "MetaCharacters"    =>      [
                "0"     =>      "/x|y/",
                "1"     =>      "/[^a-z]/",
            ],
            "form"      =>      [
                "0"     =>      "/^[0-9a-zA-Z_]+$/",     //匹配字符串必须是 字母，数字，下划线的集合
                "1"     =>      "/^[0-9a-zA-Z_]{6,20}$/"    //如上，限制字符未6-20位
            ],
            "pattern"   =>      [
                "0"     =>      "/(haha)/",
                "1"     =>      "/(hahaz)/",
                "2"     =>      "/(haa)/",
                "3"     =>      "/(windows)(?=98|95|NT|2000)/",     //正向排查 从匹配结果开始正向排查
                "4"     =>      "/(windows)(?!98|95|NT|2000)/",     //负向排查
            ],
            "practice" =>       [
                "0"     =>      "/^[a-z][0-9]$/",
                "1"     =>      "/^.s$/",
                "2"     =>      "/.s$/",
                "3"     =>      "/[A-Z]/",
                "4"     =>      "/[[:alpha:]]/",
                "5"     =>      "/^\-?[0-9]+$/",            //匹配所有整数
                "6"     =>      "/^\-?[0-9]+\.?[0-9]*$/",    //匹配所有浮点数
                "7"     =>      "/[a-z-]/",                 //匹配所有小写字母以及连接符横杠
                "8"     =>      "/[-a-z]/",
                "9"     =>      "/^\-?(?=0\.|[1-9][0-9]+|0$)[0-9]+\.?[0-9]*$/",        //修正后的匹配所有浮点数，剔除了类似于 00.20的数，若整数部分为0 ，则必须为 0.* 格式 或者 0
                "10"    =>      "/[!--]/",
                "11"    =>      "/\b([a-z]+) \1\b/",
            ],
        ];


        //测试正则字符串数组
        $strArr = [
            "tong"      =>      [
                "0"     =>      "zooo",
                "1"     =>      "zooozz",
                "2"     =>      "zookk"
            ],
            "Chapter"   =>      [
                "0"     =>      "Chapter 11",
                "1"     =>      "Chapter 220",
                "2"     =>      "Chapter 0",
                "3"     =>      "Chapter 990",
                "4"     =>      "Chapter 8800",
            ],
            "greedy"    =>      [
                "0"     =>      "<h2>",
                "1"     =>      "<h322>",
                "2"     =>      "<h1>测试贪婪</h1>",
                "3"     =>      "<h1>"
            ],
            "between"   =>      [
                "0"     =>      "testhaha",
                "1"     =>      "hahatest",
                "2"     =>      "hahtestzz"
            ],
            "repeat"    =>      [
                "0"     =>      "Is is the cost of of gasoline going up up"
            ],
            "MetaCharacters"    =>      [
                "0"     =>      "xxx",
                "1"     =>      "yyy",
                "2"     =>      "223534",
                "3"     =>      "sskj",
            ],
            "form"          =>      [
                "0"     =>      "strstr0220__",
                "1"     =>      "str0s0s0__!",
                "2"     =>      "sss!!",
                "3"     =>      "sss",
            ],
            "pattern"   =>      [
                "0"     =>      "hahaws",
                "1"     =>      "windows22windows98",
                "2"     =>      "windows98windows23",
                "3"     =>      "windows23",
                "4"     =>      "windows98",
            ],
            "practice"  =>      [
                "0"     =>      "a8",
                "1"     =>      "ab2",
                "2"     =>      "ss",
                "3"     =>      "asss",
                "4"     =>      "asbZ",
                "5"     =>      "zzzzz",
                "6"     =>      "234",
                "7"     =>      "234s",
                "8"     =>      "-23",
                "9"     =>      "-",
                "10"    =>      "0.20",
                "11"    =>      "00.20",
                "12"    =>      "02.20",
                "13"    =>      "203933.10",
                "14"    =>      "20",
                "15"    =>      "00",
                "16"    =>      "0",
                "17"    =>      "!",
                "18"    =>      ",",
                "19"    =>      "a this this a",
            ],
        ];

        /*

        preg_replace($pregArr["repeat"]["0"],$strArr["repeat"]["0"],0,100);

        echo $strArr["repeat"]["0"];

        */

        $res = preg_match($pregArr["practice"]["11"],$strArr["practice"]["19"],$matches);

        var_dump($res,$matches);
    }
}















